package com.syx.system.Service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.syx.commoncore.Model.Result;
import com.syx.commoncore.Model.TableDataInfoResult;
import com.syx.commoncore.enums.Resultcode;
import com.syx.security.Exception.ServiceException;
import com.syx.system.ElasticSearch.QuestionRepository;
import com.syx.system.Manager.QuestionCacheManager;
import com.syx.system.Mapper.question.QuestionMapper;
import com.syx.system.Model.question.DTO.QuestionAddDTO;
import com.syx.system.Model.question.DTO.QuestionEditDTO;
import com.syx.system.Model.question.DTO.QuestionQueryDTO;
import com.syx.system.Model.question.Question;
import com.syx.system.Model.question.VO.QuestionDetailVO;
import com.syx.system.Model.question.VO.QuestionQueryVO;
import com.syx.system.Model.question.es.QuestionES;
import com.syx.system.Service.IQuestionService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class QuestionServiceImpl implements IQuestionService {
    @Autowired
    QuestionMapper questionMapper;

    @Resource
    QuestionRepository questionRepository;

    @Resource
    QuestionCacheManager questionCacheManager;

    @Override
    public TableDataInfoResult list(QuestionQueryDTO questionQueryDTO){
        //已经计算了数量,如果前端传输的pageNum和size为空的话
        if (questionQueryDTO.getPageNum()==null|| questionQueryDTO.getPageSize()==null){
            questionQueryDTO.setPageNum(1);
            questionQueryDTO.setPageSize(10);
        }

        PageHelper.startPage(questionQueryDTO.getPageNum(),questionQueryDTO.getPageSize());

        List<QuestionQueryVO> questionQueryVO = questionMapper.selectQuestionList(questionQueryDTO);
        if (questionQueryVO==null || questionQueryVO.isEmpty()){
            return TableDataInfoResult.empty();
        }

        //new PageInfo<>(questionQuery).getTotal();//获取符合条件的数据总数,也就是数据库中所有的数据

        return TableDataInfoResult.success(questionQueryVO,new PageInfo<>(questionQueryVO).getTotal());//获取符合条件的数据总数);

    }


    @Override
    public Result<Void> add(QuestionAddDTO questionAddDTO) {
        List<Question> questions = questionMapper.selectList(new LambdaQueryWrapper<Question>().eq(Question::getTitle, questionAddDTO.getTitle()));
        if (CollectionUtil.isNotEmpty(questions)){
            //如果已经有一摸一样的数据，就报错
            throw new ServiceException(Resultcode.FAILED_ALREADY_EXISTS);
        }
        Question question=new Question();
        //前面是将dto中的对象转成question
        BeanUtil.copyProperties(questionAddDTO,question);
        int row = questionMapper.insert(question);
        if (row<=0){
            throw new ServiceException(Resultcode.FAILED_INSERT.toString());


        }
        //插入成功
        QuestionES questionES=new QuestionES();
        BeanUtil.copyProperties(question,questionES);
        questionRepository.save(questionES);
        questionCacheManager.addCache(question.getQuestionId());
        return Result.success();



    }

    @Override
    public Result<QuestionDetailVO> detail(Long questionId) {
        Question question = questionMapper.selectOne(new LambdaQueryWrapper<Question>().eq(Question::getQuestionId, questionId));

        //System.out.println(question);
        if (question==null){
            //如果没查询到
            throw new ServiceException(Resultcode.FAILED_NOT_EXISTS.getMessage());

        }
        QuestionDetailVO questionDetailVO=new QuestionDetailVO();

        BeanUtil.copyProperties(question,questionDetailVO);

        return Result.success(questionDetailVO);




    }

    @Override
    public Result<Void> edit(QuestionEditDTO questionEditDTO) {

        Question question=new Question();
        BeanUtil.copyProperties(questionEditDTO,question);
        //查询出来的question
        Question selectedQuestion = questionMapper.selectById(question.getQuestionId());
        if (selectedQuestion==null){
            //资源不存在
            log.warn(Resultcode.FAILED_NOT_EXISTS.toString());
            throw new ServiceException(Resultcode.FAILED_NOT_EXISTS.getMessage());

        }
        //如果查询出来的话
        int row = questionMapper.updateById(question);
        if (row!=1){
            //更新失败
            throw new ServiceException(Resultcode.FAILED.getMessage());

        }
        QuestionES questionES=new QuestionES();
        BeanUtil.copyProperties(selectedQuestion,questionES);
        questionRepository.save(questionES);





        return Result.success();

    }

    @Override
    public Result<Void> deleteQuestion(Long questionId) {
        Question selectedQuestion = questionMapper.selectOne(new LambdaQueryWrapper<Question>().eq(Question::getQuestionId,questionId));

        if (selectedQuestion==null){
            throw new ServiceException(Resultcode.FAILED_DELETE.getMessage());
        }
        int row = questionMapper.delete(new LambdaQueryWrapper<Question>().eq(Question::getQuestionId,questionId));
        if (row!=1){
            throw new ServiceException(Resultcode.FAILED_DELETE.getMessage());
        }
        questionCacheManager.deleteCache(questionId);
        questionRepository.deleteById(questionId);

        return Result.success();
    }

    /*@Override
    public Result<Void> delete(Long questionId) {
        Question selectedQuestion = questionMapper.selectById(questionId);
        if (selectedQuestion==null){
            throw new ServiceException(Resultcode.FAILED_DELETE.getMessage());
        }
        int row = questionMapper.deleteById(questionId);
        if (row!=1){
            throw new ServiceException(Resultcode.FAILED_DELETE.getMessage());
        }
        return Result.success();
    }*/




    /*@Override
    public Result<Void> delete(Long questionId) {
        Question selectedQuestion = questionMapper.selectById(questionId);
        if (selectedQuestion==null){
            throw new ServiceException(Resultcode.FAILED_DELETE.getMessage());
        }
        int row = questionMapper.deleteById(questionId);
        if (row!=1){
            throw new ServiceException(Resultcode.FAILED_DELETE.getMessage());
        }
        return Result.success();


    }*/


}
